﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
   public class MessageAdminController:  Controller
    {

      private readonly IMessagesBiz _messageService;

      public MessageAdminController(IMessagesBiz messageService)
      {
            this._messageService = messageService;
      }

     [Authorize]
     public ActionResult List(int? page=1)
     {
           ViewBag.Model = "Messages";

           var size = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["AdminpageSize"]);

            var totalRecord = 0;
            var news = _messageService.GetItemsByPages(out totalRecord,pageIndex:page.GetValueOrDefault(),pageSize:size,orderFiled:"");

            var item = new PageItem()
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages= (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
                               ControllerName = "MessageAdmin",
                               ActionName = "List"
                           };

            var viewModels = new Pager<Messages>() {DataSource = news, Item = item};

            return View(viewModels);
       }

     [Authorize]
     public ActionResult Delete(int? id = 1)
     {
         _messageService.Delete(id.GetValueOrDefault());
         return RedirectToAction("List");
     }
    }
}
